<%-- 
    Document   : index
    Created on : 04.06.2011, 18:51:52
    Author     : User
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.StringTokenizer,java.net.URL,java.io.DataInputStream,java.io.InputStreamReader,java.io.BufferedReader;"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Roster generator</title>
    </head>
    <body>
        <h1>
            Choose your army
        </h1>
        <form name="input" action="./sheet" method="post">
            <select name="race">
                <%
                    try 
                    {
                        ClassLoader cl = this.getClass().getClassLoader();
                        BufferedReader br = new BufferedReader(new InputStreamReader(cl.getResourceAsStream("serviceLocation.yml")));
                        String restUrl = "";
                        while(restUrl == "")
                        {
                            String line = br.readLine();
                            if (line == null) break;
                            if (line.trim().startsWith("location:"))
                                restUrl = line.substring(9).trim();
                        }
                        URL rest = new URL(restUrl + "army/variant");
                        // TODO process result here
                        String armyList = new DataInputStream(rest.openStream()).readLine();
                        //out.println("Result = "+result);
                    
                        //String armyList = service.armyChoice();
                        StringTokenizer st = new StringTokenizer (armyList," ");

                        while(st.hasMoreTokens())
                        {
                            String s=st.nextToken();
                            out.println(String.format("<option value=\"%s\">%s</option>", s,s));
                        }
                    } 
                    catch (Exception ex) 
                    {
                        // TODO handle custom exceptions here
                        out.println(ex.toString());
                    }
                %>
            </select>
            <button type="submit" name="submitRace" >Ok</button>

        </form>
    </body>
</html>
